package util.nlp;

import java.util.Hashtable;
import java.util.LinkedList;

import util.io.FileInput;

public class HashDictionary {

	private Hashtable<String, LinkedList<String>> dict = new Hashtable<String, LinkedList<String>>();

	/**
	 * Builds a dictionary based on a input file with the entries formatted as
	 * follow:
	 * 
	 * key 'TAB' value1 TAB value2 .....
	 * 
	 * @param path
	 */
	public HashDictionary(String path) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = line.split("\t");

			LinkedList<String> list = new LinkedList<String>();

			for (int i = 1; i < t.length; i++) {
						
				list.add(t[i].trim());

			}

			if (t.length > 0){
				
				
				if(!dict.containsKey(t[0])){
				dict.put(t[0], list);
				
				}else{
					LinkedList<String> list2 = dict.get(t[0]);
					list2.addAll(list);
				}
			}
				

			line = in.readString();
		}

	}

	public HashDictionary(String path, boolean cvs) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = line.split("\t");

			LinkedList<String> list = new LinkedList<String>();

			if (t != null && t.length > 0) {

				if (t[0].startsWith("\"") && t[0].endsWith("\"")) {

					t[0] = t[0].substring(1, t[0].length() - 1);

				}

			}

			for (int i = 1; i < t.length; i++) {

				if (cvs) {
					t[i] = t[i].trim();
					if (t[i].startsWith("\"") && t[i].endsWith("\"")) {

						t[i] = t[i].substring(1, t[i].length() - 1);
						// System.out.println(t[0]+ "\t" + t[i]);
					}

				}

				list.add(t[i].trim());

			}

			if (t.length > 0)
				dict.put(t[0], list);

			line = in.readString();
		}

	}

	public LinkedList<String> getEntries(String key) {

		return dict.get(key);

	}

}
